//
//  KLOrderDetailViewController.m
//  KunLun
//
//  Created by Mac on 2018/1/15.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "KLOrderDetailViewController.h"
#import "KLDeflutAddressView.h"
#import "KLOrderModel.h"
#import "KLOrderTableViewCell.h"
#import "KLOrderHeaderView.h"
#import "STTableEasyModel.h"
@interface KLOrderDetailViewController ()
@property(nonatomic, strong) KLDeflutAddressView                     *header;
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@property(nonatomic, strong) NSMutableArray<KLOrderGoodModel*>                     *goodsArray;
@property(nonatomic, strong) NSDictionary                     *responseDic;
@end

@implementation KLOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
    [self sendOrderDetailRequest];
    
    //注册重用
    [self.tableView registerClass:[KLOrderHeaderView class] forHeaderFooterViewReuseIdentifier:@"header"];
    [self.tableView registerClass:[KLOrderTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // Do any additional setup after loading the view.
}
- (void)configHeaderViewWithAddress:(NSString*)add name:(NSString*)name phone:(NSString*)phone{
    self.header = [[KLDeflutAddressView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 20)];
    STAddressModel * model = [STAddressModel  new];
    model.telephone = phone;
    model.name = name;
    model.address = add;
    model.city = @"";
    model.province = @"";
    self.header.model = model;
    self.tableView.tableHeaderView = self.header;
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return self.goodsArray.count;
    }
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
      return [KLOrderTableViewCell CellHeight];
    }
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return [KLOrderHeaderView headerHeight];
    }
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        KLOrderHeaderView * header = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
        [header.iconButton setTitle:self.responseDic[@"shopName"] forState:UIControlStateNormal];
        return header;
    }
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        KLOrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.backgroundColor = [UIColor whiteColor];
        cell.model = self.goodsArray[indexPath.row];
        return cell;
    }else{
        NSString * reuseIdentifier = @"sectionTwo";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
            
        }
        [cell setSt_tableEasyModel:self.dataSouce[indexPath.row]];
        cell.accessoryType = UITableViewCellAccessoryNone;
        return cell;
    }

}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        KLOrderGoodModel * model = self.goodsArray[indexPath.row];
        KLGoodDetailViewController * vc = [KLGoodDetailViewController new];
        vc.goodID = model.goods_id.description;
        [self.navigationController pushViewController:vc animated:YES];
    }

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (void)configDataSouceWithResponseDic:(NSDictionary*)dic{
    self.dataSouce = [NSMutableArray new];
    
    STTableEasyModel * model =  [[STTableEasyModel alloc] initWithTextString:@"订单信息" detailString:@""];
    model.accessoryType = UITableViewCellAccessoryNone;
    [self.dataSouce addObject:model];
    
    NSString * name = dic[@"name"];
    STTableEasyModel * nameModel =  [[STTableEasyModel alloc] initWithTextString:@"买家姓名" detailString:name];
    [self.dataSouce addObject:nameModel];
    
    NSNumber * status = dic[@"order_status"];
    NSString * orderDes = @"";
//    待发货，2.待收货，3.订单完成

    if (status.integerValue == 1) {
        orderDes = @"待发货";
    }
    if (status.integerValue == 2) {
        orderDes = @"待收货";
    }
    if (status.integerValue ==3) {
        orderDes = @"已完成";
    }
    STTableEasyModel * statusModel =  [[STTableEasyModel alloc] initWithTextString:@"支付状态" detailString:orderDes];
    [self.dataSouce addObject:statusModel];
    
    
    NSString * orderNo = dic[@"order_sn"];
    STTableEasyModel * orderNoModel =  [[STTableEasyModel alloc] initWithTextString:@"订单编号" detailString:orderNo];
    [self.dataSouce addObject:orderNoModel];
    
    NSNumber * pay_time = dic[@"created_at"];
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:pay_time.integerValue].st_localDate;
    STTableEasyModel * timeModel =  [[STTableEasyModel alloc] initWithTextString:@"下单时间" detailString:date.st_yyyyMMddhhmmssWithDate];
    [self.dataSouce addObject:timeModel];
    
    
    NSString * shopPhone = dic[@"shopPhone"];
    STTableEasyModel * shopPhoneModel =  [[STTableEasyModel alloc] initWithTextString:@"商家联系电话" detailString:shopPhone];
    [self.dataSouce addObject:shopPhoneModel];
    
}
#pragma mark --NetWork Method
- (void)sendOrderDetailRequest{
    if (!self.orderID.length) {
        return;
    }
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/shop/orderinfo",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    [dic setObject:self.orderID forKey:@"id"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic =  responseObject[@"data"][@"order_info"];
                                                 NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 NSString * address = dataDic[@"address"];
                                                 NSString * name = dataDic[@"name"];
                                                 NSString * telephone = dataDic[@"telephone"];
                                                 
                                                 // 商品
                                                 self.goodsArray = [NSMutableArray new];
                                                 NSArray * array = dataDic[@"goodsorder"];
                                                 for (NSDictionary * modelDic in array) {
                                                     KLOrderGoodModel * goodModel =[KLOrderGoodModel mj_objectWithKeyValues:modelDic ];
                                                     [self.goodsArray addObject:goodModel];
                                                 }
                                                 self.responseDic = dataDic;
                                                 [self configHeaderViewWithAddress:address name:name phone:telephone];
                                                 [self configDataSouceWithResponseDic:dataDic];
                                                 [self.tableView reloadData];
                     
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 
                                                 
                                             }];
}

@end
